package com.wxy.school.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.wxy.school.common.R;
import com.wxy.school.dto.CollectDto;
import com.wxy.school.dto.ShopDto;
import com.wxy.school.entity.Collect;
import com.wxy.school.entity.Dish;
import com.wxy.school.entity.Shop;
import com.wxy.school.service.CollectService;
import com.wxy.school.service.DishService;
import com.wxy.school.service.ShopService;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 王祥宇
 * @projectName school
 * @packageName com.wxy.school.controller
 * @data 2023/4/16 18:32
 * @description: 店铺收藏Controller管理层
 * @version: 1.0
 */
@RestController
@RequestMapping("school/collect")
public class CollectController {
    @Resource
    private CollectService collectService;

    @Resource
    private DishService dishService;

    @Resource
    private ShopService shopService;

    /**
     * @description: 添加收藏的店铺信息
     * @author: 王祥宇
     * @param: collect
     * @return: com.wxy.school.common.R<java.lang.String>
     */
    @PostMapping("/addCollect")
    public R<String> add(@RequestBody Collect collect){
        collectService.save(collect);
        return R.success("添加成功");
    }

    /**
     * @description: 根据用户id 和店铺id删除对应的收藏信息
     * @author: 王祥宇
     * @param: userId
     * @param: shopId
     * @return: com.wxy.school.common.R<java.lang.String>
     */
    @DeleteMapping("deleteCollect")
    public R<String> delete(Long userId, Long shopId){
        LambdaQueryWrapper<Collect> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Collect::getShopId, shopId);
        lambdaQueryWrapper.eq(Collect::getUserId, userId);
        collectService.remove(lambdaQueryWrapper);
        return R.success("已取消收藏");
    }

    /**
     * @description: 根据用户id查询收藏信息
     * @author: 王祥宇
     * @param: userId
     * @return: com.wxy.school.common.R<java.util.List<com.wxy.school.dto.CollectDto>>
     */
    @GetMapping("/getCollect/{userId}")
    public R<List<CollectDto>> get(@PathVariable Long userId){
        LambdaQueryWrapper<Collect> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Collect::getUserId, userId);
        // 根据用户id查询collect表中所有的该用户的收藏信息
        List<Collect> collectList = collectService.list(lambdaQueryWrapper);
        List<CollectDto> collectDtoList = collectList.stream().map((item) -> {
            CollectDto collectDto = new CollectDto();
            BeanUtils.copyProperties(item, collectDto);
            LambdaQueryWrapper<Shop> shopLambdaQueryWrapper = new LambdaQueryWrapper<>();
            shopLambdaQueryWrapper.eq(Shop::getId, item.getShopId());
            // 根据collect表中的shopId查询该收藏的店铺信息
            Shop shop = shopService.getOne(shopLambdaQueryWrapper);
            ShopDto shopDto = new ShopDto();
            BeanUtils.copyProperties(shop, shopDto);
            LambdaQueryWrapper<Dish> dishLambdaQueryWrapper = new LambdaQueryWrapper<>();
            // 根据shopId在dish表中查询所有该商店的所有菜品信息 并根据点击量进行降序排列点击量
            dishLambdaQueryWrapper.eq(Dish::getShopId, shop.getId());
            dishLambdaQueryWrapper.orderByDesc(Dish::getRate);
            shopDto.setDishList(dishService.list(dishLambdaQueryWrapper));
            collectDto.setShopDto(shopDto);
            return collectDto;
        }).collect(Collectors.toList());
        return R.success(collectDtoList);
    }

    /**
     * @description: 根据用户id和店铺id查询具体的某一个收藏信息
     * @author: 王祥宇
     * @param: userId
     * @param: shopId
     * @return: com.wxy.school.common.R<java.lang.String>
     */
    @GetMapping("/getOneCollect")
    public R<String> getOne(Long userId, Long shopId){
        LambdaQueryWrapper<Collect> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Collect::getShopId, shopId);
        lambdaQueryWrapper.eq(Collect::getUserId, userId);
        Collect collect = collectService.getOne(lambdaQueryWrapper);
        if (collect == null){
            return R.success("failed");
        }else {
            return R.success("success");
        }
    }
}
